package com.atguigu.controller;

import com.atguigu.domain.Product;
import com.atguigu.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("findAll.do")
    @RolesAllowed("ADMIN")
    public ModelAndView findAll(@RequestParam(name = "page",required = true,defaultValue = "1")Integer page, @RequestParam(name = "size",required = true,defaultValue = "4")Integer size){
        ModelAndView mv = new ModelAndView();
        List<Product> products = productService.findAllProduct(page,size);
        mv.addObject("productList", products);
        mv.setViewName("product-list");
        return mv;
    }


    @RequestMapping("save.do")
    public String saveProduct(Product product){
        productService.saveProduct(product);
        return "redirect:findAll.do";
    }
}
